package mail_operations;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import data.ConfigInfo;
import data.FilePath;

import GUIMain.GUIFocusManager;

public class TemplateSelect {
	protected static final File TEMPLATE_FOLDER_PATH = null;
	private static JDialog dialog;
	private static GridBagConstraints gbc;
	private static JButton cancel;
	private static JButton select;
	private static JComboBox templateCombo;
	private static DefaultComboBoxModel dcbm;
	private static boolean initialized = false;
	
	public static void open(File file){
		if(!initialized)
			initialize();
		loadFileList();
		
		if(file!=null)
			templateCombo.setSelectedItem(file.getName());
		else
			templateCombo.setSelectedIndex(-1);
		GUIFocusManager.add(dialog);
	}
	private static void initialize(){
		if(!initialized){
			initialized = true;
			dialog = new JDialog();
			gbc = new GridBagConstraints();
			dialog.setLayout(new GridBagLayout());
			cancel = new JButton("Cancel");
			select = new JButton("Select");
			dcbm = new DefaultComboBoxModel();
			templateCombo = new JComboBox(dcbm);
			templateCombo.setEditable(true);
			
			gbc.insets.top = 2;
			gbc.insets.bottom = 2;
			gbc.insets.left = 2;
			gbc.insets.right = 2;
			gbc.gridwidth =2;
			gbc.gridheight =1;
			gbc.gridx =0;
			gbc.gridy =0;
			dialog.add(new JLabel("Select a template or enter a the name for a new file"),gbc);
			gbc.gridwidth =1;
			gbc.gridy =1;
			gbc.gridx =0;
			dialog.add(new JLabel("Template File"),gbc);
			gbc.gridx = 1;
			dialog.add(templateCombo,gbc);
			
			JPanel buttonPanel = new JPanel();
			buttonPanel.setLayout(new GridBagLayout());
			gbc.gridx =0;
			gbc.gridy = 0;
			buttonPanel.add(cancel,gbc);
			gbc.gridx = 1;
			buttonPanel.add(select,gbc);
			
			gbc.gridwidth =2;
			gbc.gridx = 0;
			gbc.gridy = 2;
			dialog.add(buttonPanel,gbc);
			
			addListeners();
			
			GUIFocusManager.setDialogProperties(dialog);
		}
	}
	private static void loadFileList(){
		dcbm.removeAllElements();
		
		File file = new File(FilePath.TEMPLATE_FOLDER_PATH);
		if(!file.exists() || !file.isDirectory()){
			file.mkdirs();
			return;
		}
		FileFilter f = new FileFilter() {
			@Override
			public boolean accept(File arg0) {
				if(arg0.getName().substring(arg0.getName().length()-3, arg0.getName().length()).toUpperCase().equals("MST")){
					return true;
				}
				return false;
			}
		};
		File[] fileList = file.listFiles(f);
		for(int i=0;i<fileList.length;i++){
			dcbm.addElement(fileList[i].getName());
		}
	}
	private static void addListeners(){
		cancel.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				GUIFocusManager.remove();
			}
		});
		select.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				Object z = templateCombo.getSelectedItem();
				//The user has entered something
				if(z!=null){
					String s = (String)z;
					s.replace(" ", "_");
					File f = null;
					//Add file extension if it does not exist
					if(!s.contains(".mst"))
						f = new File(FilePath.TEMPLATE_FOLDER_PATH+s+".mst");
					else
						f = new File(FilePath.TEMPLATE_FOLDER_PATH+s);
					if(!f.exists()){
						int selection = JOptionPane.showConfirmDialog(null, "File " + f.getName() + 
								" does not exist, do you want to create a new file?", 
								"New File", JOptionPane.YES_NO_OPTION);
						switch(selection){
							case JOptionPane.YES_OPTION:
							try {
								f.createNewFile();
								MailerGUI.openNewFile(f);
								GUIFocusManager.remove();
							} catch (IOException e1) {
								JOptionPane.showMessageDialog(null, "Template creation failed! Ensure your filename does not contain invalid characters and try again");
								templateCombo.requestFocus();
								e1.printStackTrace();
							}
								break;
							case JOptionPane.NO_OPTION:
								break;
						}
					}else{
						//Load file
						boolean success = MailerGUI.parseSavedEmail(f);
						if(success){
							ConfigInfo.LAST_OPEN_TEMPLATE = f.getName();
							GUIFocusManager.remove();
						}else{
							JOptionPane.showMessageDialog(null,"An error opening file " + f.getName() + " has occurred");
						}
					}
				}else{
					JOptionPane.showMessageDialog(null, "Please select or enter a template file name");
					templateCombo.requestFocus();
				}
			}
		});
	}

}
